*****************************************************************
*This file reshapes completions by field of study from the IPEDS*
*****************************************************************

global yearList 1989 1990 1991 1992 1993 1994 1995 1999 2000 2001 2006 2007 2008 2013 2014 2015

*foreach i in $yearList {
foreach i in 1990 1994 2000 2007 2014 {
use "$clean_data_education/completions.dta", clear

*** Build three year averages
* year group
gen yearGroup = 0
	replace yearGroup = 1990 if year <= 1991 
	replace yearGroup = 1994 if year >= 1992 & year <= 1994
	replace yearGroup = 2000 if year >= 1999 & year <= 2001
	replace yearGroup = 2007 if year >= 2006 & year <= 2008
	replace yearGroup = 2014 if year >= 2013
	drop if yearGroup == 0 // 1992
	
keep if yearGroup == `i'
drop cipcode

**********************************************************
* Run it all together, save labels and collapse
foreach v of var * {
local l`v' : variable label `v'
	if `"`l`v''"' == "" {
	local l`v' "`v'"
  	}
}
*keep if awlevel < 5 // if you want to keep only bachelor's
*keep if awlevel == 3 // if you want to keep only associate degrees 
*keep if awlevel >= 3 & awlevel <=4 // if you want to keep associate degrees & 2Y but < 4Y
keep if awlevel >= 2 & awlevel <=4 // if you want to keep only 2Y institutions
collapse (sum) blackMen blackWomen hispanicMen hispanicWomen whiteMen whiteWomen black white hispanic total men women (last) yearGroup, by(unitid majors year) // aggregating cipcodes
collapse (mean) blackMen blackWomen hispanicMen hispanicWomen whiteMen whiteWomen black white hispanic total men women, by(unitid majors yearGroup)
foreach v of var * {
label var `v' "`l`v''"
}
**********************************************************

reshape wide blackMen blackWomen hispanicMen hispanicWomen whiteMen whiteWomen black white hispanic total men women, i(unitid) j(majors) 


save "$clean_data_education/Temporary4_`i'.dta", replace
}


* append
use "$clean_data_education/Temporary4_1990.dta", clear
foreach i in 1994 2000 2007 2014 {
append using "$clean_data_education/Temporary4_`i'.dta"
rm "$clean_data_education/Temporary4_`i'.dta"
}
rm "$clean_data_education/Temporary4_1990.dta"

foreach v of var black* hispanic* white* men* women* total* {
replace `v' = 0 if `v' == . 
}
save "$clean_data_education/completionsMajorReshaped.dta", replace

